Recording Outputs And Simulating
Recording Outputs
Outputs that are generated by processing (via logic, math, or just raw values) inputs from the inputs class can be used to simulate code changes by CHANGING the logic or math that generates the inputs, feeding in the inputs again (by running the simulation program), and viewing the outputs that are produced.
This is done using the code:
Logger.RecordOutput("Name", value);
This code can be written after the inputs have been recorded, and you don't need a robot for the following simulation to work. As long as the recorded output is determined using code performed with values from the recorded inputs, the value will be determined by new code written before simulating.
Performing the Simulation
By Replay Simulation, we mean that we are testing what outputs our new code would have produced if it was given the inputs from a past match. These inputs are saved on a log file during the match, as long as the code from every step until this was on the robot during the match. Here are the steps for performing Replay simulation:
- Open the log file from the match whose inputs you will use. Since multiple log files are generated from each match (each containing data in different formats), choose the one that starts with "akit_" and ends with ".wpilog" on AdvantageScope.
- from VSCode, run the task "WPILib: Simulate robot code". This task is found in the same places that the "WPILib: Deploy Robot Code" and "WPILib: Build Robot Code" tasks are found. If the code builds succesfully, you will be asked to select which extensions you want to open. I like to open none of them. I haven't found them very helpful, and they aren't necessary
- If the code is able to run through the simulation without crashing, a new log file will be created. This one will have the same name as the original, but with "_sim" near the end. This file contains the recorded inputs, and the RealOutputs folder with all recorded outputs (just like the original log file), but also contains a ReplayOutputs folder, with the RealOutputs that would have been generated if the same inputs were ran through the new code
NOTE: when you run a simulation for the first time on a new device, a file called networktables.json is created. This file contains the Preferences that the could uses, which it usually gets from the rio when it is connected to a robot. These preferences have default values that can easily be changed. If the Preference values are entered incorrectly, it can crash code or cause some outputs to not be generated, depending on how the Preferences are checked within the code.